package io.github.junxworks.qt.modules.mm.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;

import io.github.junxworks.ep.core.Result;
import io.github.junxworks.ep.core.utils.PageUtils;
import io.github.junxworks.ep.sys.annotations.EpLog;
import io.github.junxworks.ep.sys.constants.RecordStatus;
import io.github.junxworks.junx.core.util.StringUtils;
import io.github.junxworks.qt.modules.mm.dto.MmDatastreamCondition;
import io.github.junxworks.qt.modules.mm.dto.MmDatastreamDto;
import io.github.junxworks.qt.modules.mm.service.MmDatastreamService;

/**
 * mm_datastream Entity Controller Class
 *
 * @since Dec 9, 2021 10:38:22 PM Generated by JunxEP
 */
@RestController
@RequestMapping("/mm/datastreams")
public class MmDatastreamController {
	@Autowired
	private MmDatastreamService mmDatastreamService;

	/**
	 * Query list.
	 *
	 * @param condition the condition
	 * @return the result
	 */
	@GetMapping()
	public Result queryMmDatastreamList(MmDatastreamCondition condition) {
		//查询列表数据
		PageUtils.setPage(condition);
		return Result.ok(new PageInfo<>(mmDatastreamService.queryList(condition)));
	}

	/**
	 * Save .
	 *
	 * @param dto the dto
	 * @return the result
	 */
	@EpLog("模型-数据流-保存数据流")
	@PostMapping()
	public Result saveMmDatastream(@RequestBody MmDatastreamDto dto) {
		mmDatastreamService.save(dto);
		return Result.ok();
	}

	/**
	 * Query entity by id.
	 *
	 * @param id the id
	 * @return the result
	 */
	@GetMapping("/{id}")
	public Result queryMmDatastreamEntityById(@PathVariable("id") Long id) {
		return Result.ok(mmDatastreamService.queryMmDatastreamById(id));
	}

	@GetMapping("/{id}/fields")
	public Result queryMmDsFieldsById(@PathVariable("id") Long id) {
		return Result.ok(mmDatastreamService.queryFieldsByDsId(id));
	}

	/**
	 * Delete.
	 *
	 * @param id the id
	 * @return the result
	 */
	@EpLog("模型-数据流-删除数据流")
	@DeleteMapping("/{id}")
	public Result deleteMmDatastream(@PathVariable("id") Long id) {
		String msg = mmDatastreamService.checkDsCanDelete(id);
		if (StringUtils.notNull(msg)) {
			return Result.error(msg);
		}
		MmDatastreamDto dto = new MmDatastreamDto();
		dto.setId(id);
		dto.setStatus(RecordStatus.DELETED.getValue());
		mmDatastreamService.save(dto);
		return Result.ok();
	}
}